<?php ob_start(); 
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php');
require_once('../../../include/inc_config.php'); ?>
<?php $message = ""; ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$colname_rs1 = "-1";
if (isset($_GET['Identifiant_condition_vente'])) {
  $colname_rs1 = $_GET['Identifiant_condition_vente'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs1 = sprintf("SELECT * FROM conditionvente WHERE Identifiant_condition_vente = %s", GetSQLValueString($colname_rs1, "int"));
$rs1 = mysql_query($query_rs1, $microassur) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1);

mysql_select_db($database_microassur, $microassur);
$query_rs_offre = "SELECT * FROM offre";
$rs_offre = mysql_query($query_rs_offre, $microassur) or die(mysql_error());
$row_rs_offre = mysql_fetch_assoc($rs_offre);
$totalRows_rs_offre = mysql_num_rows($rs_offre);

mysql_select_db($database_microassur, $microassur);
$query_rs_produit = "SELECT * FROM produit";
$rs_produit = mysql_query($query_rs_produit, $microassur) or die(mysql_error());
$row_rs_produit = mysql_fetch_assoc($rs_produit);
$totalRows_rs_produit = mysql_num_rows($rs_produit);

// Contrôle de saisie des Dates DEBUT/FIN
$lesdates=verifDate(@$_POST['Date_debut_condition_vente'],@$_POST['Date_fin_condition_vente']);

// Vérifier si un même code existe déjà dans la table conditionvente

	if(isset($_POST['Code_condition_vente']) && isset($_POST['Valeur_condition_vente']))
	{
		// meme code et meme valeur  --  THIA
        /*// --- si le code existe pour l'offre
        $toCriterias['Valeur_condition_vente'] = $_POST['Valeur_condition_vente'] ;
        $toCriterias['Code_condition_vente'] = $_POST['Code_condition_vente'] ;
        $toCriterias['Identifiant_offre'] = $_POST['Identifiant_offre'] ;
        $existeOffre = existeEnregistrement($database_microassur, $microassur, "conditionvente", $toCriterias) ;
        
        // --- si le code existe pour le produit
        $toCriterias['Identifiant_produit'] = $_POST['Identifiant_produit'] ;
        unset($toCriterias['Identifiant_offre']) ;
        $existeProduit = existeEnregistrement($database_microassur, $microassur, "conditionvente", $toCriterias) ;
        
        $existe = $existeOffre && $existeProduit ; */ 
		// $existe = existeDeuxChamp($database_microassur, $microassur, 'conditionvente', 'Code_condition_vente', 'Valeur_condition_vente', @$_POST['Code_condition_vente'], @$_POST['Valeur_condition_vente']);
		$verif=veriflibelle1("Code_condition_vente");
	/************************************************/
			if($_POST['Code_condition_vente']!=NULL && $_POST['Valeur_condition_vente']!=NULL)
			{
    			/*$code_cond = $_POST['Code_condition_vente'];
    			$val_cond = $_POST['Valeur_condition_vente'];
    			$query_rs_condv = 'SELECT Identifiant_condition_vente FROM conditionvente where Code_condition_vente='.'"'. $code_cond.'"';
    			$query_rs_condv .= 'AND Valeur_condition_vente='.$val_cond;
    			$query_rs_condv1 = mysql_query($query_rs_condv, $microassur) or die(mysql_error());
    			$query_rs_condv2 = mysql_fetch_assoc($query_rs_condv1);
    			$num_row = mysql_num_rows($query_rs_condv1);
    			$num_ligne = $query_rs_condv2['Identifiant_condition_vente'];*/
                
                // --- la modification concerne autre chose que le type
                $oConditionVente = new dataObject_conditionVente($_POST['Identifiant_condition_vente']);
                $iUpdateAutre = $oConditionVente->_CODE_CDT_VENTE == $_POST['Code_condition_vente'] && $oConditionVente->_VALEUR_CDT_VENTE == $_POST['Valeur_condition_vente'] ;
                $iUpdateAutreQueOffre = $iUpdateAutre && $oConditionVente->_IDENTIFIANT_OFFRE == $_POST['Identifiant_offre'] ;
                $iUpdateAutreQueProduit = $iUpdateAutre && $oConditionVente->_IDENTIFIANT_PRODUIT == $_POST['Identifiant_produit'] ;
            
                if (!$iUpdateAutreQueOffre)
                {
                    // --- si le code existe pour l'offre
                    $toCriterias['Valeur_condition_vente'] = $_POST['Valeur_condition_vente'] ;
                    $toCriterias['Code_condition_vente'] = $_POST['Code_condition_vente'] ;
                    $toCriterias['Identifiant_offre'] = $_POST['Identifiant_offre'] ;
                    $existeOffre = existeEnregistrement($database_microassur, $microassur, "conditionvente", $toCriterias) ;
                }
                else
                {
                    $existeOffre = FALSE ;
                }
                
                if (!$iUpdateAutreQueOffre)
                {
                    // --- si le code existe pour le produit
                    $toCriterias['Valeur_condition_vente'] = $_POST['Valeur_condition_vente'] ;
                    $toCriterias['Code_condition_vente'] = $_POST['Code_condition_vente'] ;
                    $toCriterias['Identifiant_produit'] = $_POST['Identifiant_produit'] ;
                    unset($toCriterias['Identifiant_offre']) ;
                    $existeProduit = existeEnregistrement($database_microassur, $microassur, "conditionvente", $toCriterias) ;
                }
                else
                {
                    $existeProduit = FALSE ;
                }
                
                $existe = $existeOffre && $existeProduit ;
                
			}
			
	}

// Vérifier si Code_element est modifié
if(isset($_POST['Code_condition_vente'])){
$comparer = strcmp($_POST['Code_condition_vente'], $row_rs1['Code_condition_vente']);
$comparer2 = strcmp($_POST['Valeur_condition_vente'], $row_rs1['Valeur_condition_vente']);
$verif=veriflibelle1("Code_condition_vente");
}
// Contrôle de saisie des Dates DEBUT/FIN
if(isset($_POST['Date_fin_condition_vente']) && $_POST['Date_debut_condition_vente']){
	$datedebut = datevalider($_POST['Date_debut_condition_vente']);
		$datefin = datevalider($_POST['Date_fin_condition_vente']);
	$datevalide1 = datevalide($_POST['Date_debut_condition_vente']);
		$datevalide2 = datevalide($_POST['Date_fin_condition_vente']);
	$lesdates=verifDate($_POST['Date_debut_condition_vente'],$_POST['Date_fin_condition_vente']);
}
if(isset($_POST['Code_condition_vente']) && isset($_POST['Valeur_condition_vente']) && isset($_POST['Identifiant_offre']) && $existeOffre!=0){
	$message = colorer("Cette condition de vente existe déjà pour l'offre choisie","false");
}elseif(isset($_POST['Code_condition_vente']) && isset($_POST['Valeur_condition_vente']) && isset($_POST['Identifiant_produit']) && $existeProduit!=0){
	$message = colorer("Cette condition de vente existe déjà pour le produit choisi","false");
} elseif(isset($_POST['Code_condition_vente']) && empty($_POST['Code_condition_vente'])){
	$message = colorer("Vous devez saisir le code","false");
}elseif(isset($_POST['Code_condition_vente']) && !$verif){
	$message = colorer("Le code saisi comporte des caractères spéciaux interdits","false");
}elseif(isset($_POST['Libelle_condition_vente']) && empty($_POST['Libelle_condition_vente'])){
	$message = colorer(" Vous devez saisir le libellé","false");
}/*elseif(isset($_POST['Valeur_condition_vente']) && !empty($_POST['Valeur_condition_vente']) &&  !verifNumeric($_POST['Valeur_condition_vente'])){
	$message = colorer("La valeur de la condition de vente doit être numérique","false");
}*/elseif(isset($_POST['Date_debut_condition_vente']) && empty($_POST['Date_debut_condition_vente'])){
	$message = colorer("Vous devez saisir la date de début","false");
}elseif(isset($_POST['Date_debut_condition_vente']) && !$datedebut){
	$message = colorer("Le format de la date de début est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_debut_condition_vente']) && !$datevalide1){
	$message = colorer("La date de début est incorrecte ","false");
}elseif(isset($_POST['Date_fin_condition_vente']) && $_POST['Date_fin_condition_vente']!=NULL && !$datefin){
	$message = colorer("Le format de la date de fin est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_fin_condition_vente']) && $_POST['Date_fin_condition_vente']!=NULL && !$datevalide2){
	$message = colorer("La date de fin est incorrecte ","false");
}elseif(isset($_POST['Date_debut_condition_vente']) && isset($_POST['Date_fin_condition_vente'])  && !empty($_POST['Date_fin_condition_vente']) && !empty($_POST['Date_debut_condition_vente'])&& $lesdates==FALSE){
	$message = colorer("La date de fin doit être postérieure à la date de début","false");
}
else{
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
	  $updateSQL = sprintf("UPDATE conditionvente SET Code_condition_vente=%s, Libelle_condition_vente=%s, Valeur_condition_vente=%s, Type_condition_vente=%s, Date_debut_condition_vente=%s, Date_fin_condition_vente=%s, Identifiant_offre=%s, Identifiant_produit=%s WHERE Identifiant_condition_vente=%s",
						   GetSQLValueString($_POST['Code_condition_vente'], "text"),
						   GetSQLValueString($_POST['Libelle_condition_vente'], "text"),
						   GetSQLValueString($_POST['Valeur_condition_vente'], "int"),
						   GetSQLValueString($_POST['Type_condition_vente'], "text"),
						   GetSQLValueString(dateEN2FR($_POST['Date_debut_condition_vente']), "date"),
						   GetSQLValueString(dateEN2FR($_POST['Date_fin_condition_vente']), "date"),
						   GetSQLValueString($_POST['Identifiant_offre'], "int"),
						   GetSQLValueString($_POST['Identifiant_produit'], "int"),
						   GetSQLValueString($_POST['Identifiant_condition_vente'], "int"));
						   
	  mysql_select_db($database_microassur, $microassur);
	  mysql_query("START TRANSACTION");
	    	  
	  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
	  if($Result1){
	  		mysql_query("commit");
			$message = colorer("La modification de la condition de vente est prise en compte","true");
			if($_POST['Libelle_condition_vente']!=$row_rs1['Libelle_condition_vente'])
				$row_rs1['Libelle_condition_vente'] = $_POST['Libelle_condition_vente'];
			if($_POST['Valeur_condition_vente']!=$row_rs1['Valeur_condition_vente'])
				$row_rs1['Valeur_condition_vente'] = $_POST['Valeur_condition_vente'];
			if($_POST['Type_condition_vente']!=$row_rs1['Type_condition_vente'])
				$row_rs1['Type_condition_vente'] = $_POST['Type_condition_vente'];
			if($_POST['Date_debut_condition_vente']!=$row_rs1['Date_debut_condition_vente'])
				$row_rs1['Date_debut_condition_vente'] = dateEN2FR($_POST['Date_debut_condition_vente']);
			if($_POST['Date_fin_condition_vente']!=$row_rs1['Date_fin_condition_vente'])
				$row_rs1['Date_fin_condition_vente'] = dateEN2FR($_POST['Date_fin_condition_vente']);
			if($_POST['Identifiant_offre']!=$row_rs1['Identifiant_offre'])
				$row_rs1['Identifiant_offre'] = $_POST['Identifiant_offre'];
			if($_POST['Identifiant_produit']!=$row_rs1['Identifiant_produit'])
				$row_rs1['Identifiant_produit'] = $_POST['Identifiant_produit'];
		}
	  else
		{$message = colorer("Contacter la DSI, erreur modification de la condition de vente","false");mysql_query("ROLLBACK");}
	}
}
/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="T0002"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../condvente.css.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../form_ie7.css" /><![endif]-->
            <link type="text/css" href="../../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../../js/scripts.js"></script>
	</head>
<body id="cdv">
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Condition vente";
					$nommodule2="Détail condition vente";
					?>
			</div>
			<div class="cleared"></div>
			<!-- /*******************************/ -->
	<div class="corps1">
		<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
					<div class="content_tableform1">
							  <table>
								<tr>
								<td>CODE</td>
								  <td><input class="resize_1" type="text" name="Code_condition_vente" value="<?php if (isset($_POST['Code_condition_vente'])) echo remind_text('Code_condition_vente'); else echo htmlentities($row_rs1['Code_condition_vente'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="5" /></td>
								</tr>
								<tr>
								  <td>LIBELLE</td>
								  <td colspan="3"><input class="td2" type="text" name="Libelle_condition_vente" value="<?php if (isset($_POST['Libelle_condition_vente'])) echo remind_text('Libelle_condition_vente'); else echo htmlentities($row_rs1['Libelle_condition_vente'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" /></td>
								</tr>
								<tr>
								  <td>VALEUR</td>
								  <td colspan="3"><input class="td1" type="text" name="Valeur_condition_vente" value="<?php if (isset($_POST['Valeur_condition_vente'])) echo remind_text('Valeur_condition_vente'); else echo htmlentities($row_rs1['Valeur_condition_vente'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="10" /></td>
								</tr>
								<tr>
								  <td>TYPE</td>
								  <td colspan="3">
									<select class="select" name="Type_condition_vente">
									<?php do{ ?>
												<option value="<?php if (isset($_POST['Code_element'])) echo $_POST['Code_element']; else echo $row_rs_dico2['Code_element']; ?>" <?php if (!(strcmp($row_rs_dico2['Code_element'], htmlentities($row_rs1['Type_condition_vente'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_rs_dico2['Nom_element']; ?></option>
									<?php 	} while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
									</select>
								  </td>
								</tr>
								<tr>
								  <td>DATE DEBUT</td>
								  <td><input class="date" type="text" name="Date_debut_condition_vente" value="<?php if (isset($_POST['Date_debut_condition_vente'])) echo $_POST['Date_debut_condition_vente']; else echo htmlentities(dateEN2FR($row_rs1['Date_debut_condition_vente']), ENT_COMPAT, 'utf-8'); ?>" size="10" maxlength="10" /></td>
								  <td class="td">DATE FIN</td>
								  <td><input class="date" type="text" name="Date_fin_condition_vente" value="<?php if (isset($_POST['Date_fin_condition_vente'])) echo $_POST['Date_fin_condition_vente']; else echo htmlentities(dateEN2FR($row_rs1['Date_fin_condition_vente']), ENT_COMPAT, 'utf-8'); ?>" size="10" maxlength="10" /></td>
								</tr>
								<tr>
								  <td>OFFRE</td>
								  <td><select class="resize_1" name="Identifiant_offre">
									<?php 
							do {  
							?>
									<option value="<?php echo $row_rs_offre['Identifiant_offre'] ?>" <?php if (!(strcmp($row_rs_offre['Identifiant_offre'], htmlentities($row_rs1['Identifiant_offre'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_rs_offre['Libelle_offre']?></option>
									<?php
							} while ($row_rs_offre = mysql_fetch_assoc($rs_offre));
							?>
								  </select>
								  </td>
								  <td class="td">PRODUIT</td>
								  <td><select class="resize_1" name="Identifiant_produit">
								  <option value="">Sélectionner un produit</option>

									<?php
							do {  
							?>
									<option value="<?php echo $row_rs_produit['Identifiant_produit'] ?>"<?php if (!(strcmp($row_rs_produit['Identifiant_produit'], htmlentities($row_rs1['Identifiant_produit'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_produit['Libelle_produit']?></option>
									<?php
							} while ($row_rs_produit = mysql_fetch_assoc($rs_produit));
							  $rows = mysql_num_rows($rs_produit);
							  if($rows > 0) {
								  mysql_data_seek($rs_produit, 0);
								  $row_rs_produit = mysql_fetch_assoc($rs_produit);
							  }
							?>
								  </select>
								  </td>
								</tr>
							  </table>
							</div>
			</div>
					<div class="imgright"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="./index.php"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Modifier" value="Modifier" class="resizeall btmiddle"/><span class="btright"></span></div>
							
						<div class="cleared"></div>
  <input type="hidden" name="Identifiant_condition_vente" value="<?php echo $row_rs1['Identifiant_condition_vente']; ?>" />
  <input type="hidden" name="MM_update" value="form1" />
</form>
	</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs1);
mysql_free_result($rs_offre);
mysql_free_result($rs_produit);
?>
